import {
  getToken
} from '@/utils/auth'
import {
  queryList,
  queryListByParam,
  queryListForPoslist,
  getUserFromPosition,
  userQueryList,
  setPositionToOrg,
  deletePositionToOrg,
  setUserToPosition,
  deletes,
  add,
  update
} from '@/api/system/orginfo'
const orginfo = {
  state: {
    token: getToken(),
    name: '',
    avatar: '',
    roles: []
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    }
  },

  actions: {
    /**
     * 查询所有机构信息
     * @param commit
     * @param datas
     * @returns {Promise<any>}
     * @constructor
     */
    OrgQueryList({
      commit
    }, datas) {
      return new Promise((resolve, reject) => {
        queryList(datas)
          .then(response => {
            // const data = response.datas
            // setToken(data.token)
            // commit('SET_TOKEN', data.token)
            resolve(response)
          })
          .catch(error => {
            reject(error)
          })
      })
    },
    OrgQueryListByParam({
      commit
    }, datas) {
      return new Promise((resolve, reject) => {
        queryListByParam(datas)
          .then(response => {
            resolve(response)
          })
          .catch(error => {
            reject(error)
          })
      })
    },

    /**
     * 根据机构id查询已分配与未分配的岗位列表
     * @param {*} param
     * @param {*} orgid
     */
    OrgQueryListForPoslist({
      commit
    }, orgid) {
      return new Promise((resolve, reject) => {
        queryListForPoslist(orgid)
          .then(response => {
            resolve(response)
          })
          .catch(error => {
            reject(error)
          })
      })
    },

    /**
     * 设置岗位信息
     * @param {*} param
     * @param {*} datas
     */
    SetPositionToOrg({
      commit
    }, datas) {
      return new Promise((resolve, reject) => {
        setPositionToOrg(datas)
          .then(response => {
            resolve(response)
          })
          .catch(error => {
            reject(error)
          })
      })
    },

    /**
     * 撤销岗位信息
     * @param {*} param
     * @param {*} datas
     */
    DeletePositionToOrg({
      commit
    }, datas) {
      return new Promise((resolve, reject) => {
        deletePositionToOrg(datas)
          .then(response => {
            resolve(response)
          })
          .catch(error => {
            reject(error)
          })
      })
    },

    // 查询所有用户
    UserQueryList3({
      commit
    }, datas) {
      return new Promise((resolve, reject) => {
        userQueryList(datas).then(response => {
          // const data = response.data
          // setToken(data.token)
          // commit('SET_TOKEN', data.token)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    /**
     * 根据机构id与岗位id获取岗位人员信息列表
     * @param {*} param
     * @param {*} datas
     */
    GetUserFromPosition({ commit }, datas) {
      return new Promise((resolve, reject) => {
        getUserFromPosition(datas)
          .then(response => {
            resolve(response)
          })
          .catch(error => {
            reject(error)
          })
      })
    },

    /**
     * 根据组织机构ID、 岗位ID、 用户ID列表， 设置指定机构岗位下的用户信息
     * @param {*} param
     * @param {*} datas
     */
    SetUserToPosition({ commit }, datas) {
      return new Promise((resolve, reject) => {
        setUserToPosition(datas)
          .then(response => {
            resolve(response)
          })
          .catch(error => {
            reject(error)
          })
      })
    },

    /**
     * 删除机构信息
     * @param commit
     * @param ids
     * @constructor
     */
    OrgDeletes({
      commit
    }, ids) {
      return new Promise((resolve, reject) => {
        deletes(ids)
          .then(response => {
            resolve()
          })
          .catch(error => {
            reject(error)
          })
      })
    },

    /**
     * 新增机构信息
     * @param commit
     * @param datas
     * @returns {Promise<any>}
     * @constructor
     */
    OrgAdd({
      commit
    }, datas) {
      return new Promise((resolve, reject) => {
        add(datas)
          .then(response => {
            resolve(response)
          })
          .catch(error => {
            reject(error)
          })
      })
    },

    /**
     * 编辑机构信息
     * @param commit
     * @param datas
     * @returns {Promise<any>}
     * @constructor
     */
    OrgUpdate({
      commit
    }, datas) {
      return new Promise((resolve, reject) => {
        update(datas)
          .then(response => {
            resolve(response)
          })
          .catch(error => {
            reject(error)
          })
      })
    }
  }
}

export default orginfo
